var layModules = {};

function GroupTable() {

}

GroupTable.prototype = {
    search: function (nameOrNumber) {
        var _this = this;
        layModules.table.render({
            elem: '#group-table',
            height: 600,
            url: '/front/group/search?w=' + nameOrNumber,
            page: false,
            cols: [[
                {field: 'id', title: '#', width: 80, fixed: 'left'},
                {field: 'name', title: '名称', fixed: 'left'},
                {field: 'idNumber', title: '号码'},
                {field: 'description', title: '描述'},
                {
                    field: 'operation', title: '操作', templet: function (d) {
                        return '<button data-id="' + d.id + '" class="btn-join layui-btn layui-btn-xs layui-btn-normal">加入</button>';
                    }
                }
            ]],
            done: function () {
                $('.btn-join').click(function () {
                    var id = $(this).attr('data-id');
                    _this.join(id);
                });
            }
        });
    },
    join: function (id) {
        $.post('/front/group/_join', {groupId: id}, function (res) {
            if (res.code === 0) {
                Utils.showToast('加入成功')
            } else {
                Utils.showToast(res.msg);
            }
        }, 'json');
    }
};
var Utils = {
    showToast: function (msg) {
        layModules.layer.open({
            title: '提示',
            content: msg
        });
    }
};
$(function () {
    layui.use(['table', 'layer'], function () {
            layModules.table = layui.table;
            layModules.layer = layui.layer;
            var groupTable = new GroupTable();
            $('#btn-search').click(function () {
                var nameOrNumber = $('#input-name-or-number').val();
                if (nameOrNumber) {
                    groupTable.search(nameOrNumber);
                } else {
                    Utils.showToast('请输入群组的名称或者群组的号码');
                }
            });
        }
    )
});